let express = require("express");
let path = require("path");
let dbhelper = require("../lib/dbHelper");
let adminRouter = express.Router();
module.exports = adminRouter;
//注册接口
adminRouter.post("/Register", function (req, res, next) {
    let body = req.body;
    let params = [body.uname, body.upwd];
    let querySQL = "SELECT * FROM userinfo WHERE u_name=?;";
    let insertSQL = "INSERT INTO userinfo(`u_name`,`u_pwd`) VALUES(?,?);";
    console.log(body.uname)
    //验证用户名是否已经存在
    dbhelper.query(querySQL, body.uname, function (err, result) {
        if (!err) {
            if (result.length === 0) {
                //向数据库插入数据
                dbhelper.query(insertSQL, params, function (err, result) {
                    if (!err) {
                        if (result.affectedRows >= 1) {
                            res.json({
                                msg: "注册成功",
                                status: 1
                            })
                        } else {
                            res.json({
                                msg: "注册失败",
                                status: -1
                            })
                        }
                    } else {
                        res.json("insert出错了!");
                    }
                })
            } else {
                res.json({
                    msg: "用户名已存在",
                    status: 0
                })
            }
        } else {
            res.json("select出错了!");
        }
    })
})
//登录接口
adminRouter.post("/Login",function(req,res,next){
    let body = req.body;
    console.log(req);
    let params=[body.uname,body.upwd];
    let querySQL = "SELECT * FROM userinfo WHERE u_name=? AND u_pwd=?;";
    dbhelper.query(querySQL,params,function(err,result){
        if(!err){
            if(result.length>=1){
                res.json({
                    msg:"登录成功",
                    status:1,
                    id:result[0].u_id,
                    portrait:result[0].u_portrait
                })
            }else{
                console.log("用户名或密码错误!")
                res.json({
                    msg: "用户名或密码错误",
                    status: -1
                })
            }
        }else{
            res.json("query出错了!");
        }
    })
})
//首页信息显示接口
adminRouter.get("/getWorks",function(req,res,next){
    let querySQL = "SELECT w_id,w.u_id,w_img,w_content,w_title,u_name,u_portrait FROM works ";
    querySQL += "AS w INNER JOIN userinfo AS u ON w.u_id=u.u_id;";
    dbhelper.query(querySQL,[],function(err,result){
        if(!err){
            res.json(result);
        }else{
            res.json("查询作品信息出错了!");
        }
    })
})
//关注
adminRouter.post("/Follow",function(req,res,next){
    let params = [req.body.uid,req.body.fid]
    let querySQL = "SELECT `u_id`,`following` FROM user_follow WHERE u_id=? AND following=?";
    dbhelper.query(querySQL,params,function(err,result){
        if(!err){
            if(result.length===0){
                let sql = "INSERT INTO user_follow(`u_id`,`following`)VALUES(?,?);";
                dbhelper.query(sql, params, function (err, result) {
                    if (!err) {
                        if (result.affectedRows >= 1) {
                            res.json({
                                msg: "关注成功",
                                status: 1
                            })
                        } else {
                            res.json({
                                msg: "关注失败",
                                status: -1
                            })
                        }
                    }
                })
            }else{
                res.json({
                    msg:"该用户已经关注",
                    status:0
                })
            }
        }
    })
    
})
//取消关注
adminRouter.post("/unFollow",function(req,res,next){
    let params =[req.body.uid,req.body.fid];
    console.log(params);
    let sql = "DELETE FROM user_follow WHERE u_id = ? AND following=?;";
    dbhelper.query(sql,params,(err,result)=>{
        if(!err){
            console.log(result);
            if(result.affectedRows>=1){
                res.json({msg:"取消关注成功!",status:1});
            }else{
                res.json({msg:"取消关注失败!",status:-1});
            }
        }else{
            console.log("数据库操作失败!",err);
        }
    })
})
//查询某用户的关注列表
adminRouter.get("/getFollowList",function(req,res,next){
    let params = [parseInt(req.query.uid)];
    let sql = "SELECT `following`,`u_portrait`,`u_name` FROM user_follow AS f INNER JOIN ";
    sql+="userinfo AS u ON f.following=u.u_id WHERE f.u_id=102;";
    dbhelper.query(sql,params,function(err,result){
        if(!err){
            if(result.length>=1){
                res.json({
                    msg:"查询数据库成功",
                    status:1,
                    result
                });
            }else{
                res.json({
                    msg: "你还没有关注任何人",
                    status:-1
                })
            }
        }else{
            res.json({
                msg: "数据库查询失败",
                status: -1
            })
        }
    })
})